package xps.and.uudaijia.userclient.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.hitomi.cslibrary.CrazyShadow;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import nucleus.factory.RequiresPresenter;
import xps.and.uudaijia.userclient.App;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.adapter.PoiListAdapter;
import xps.and.uudaijia.userclient.presenter.ObtainAddressPresenter;
import xps.and.uudaijia.userclient.util.CommonUtils;
import xps.and.uudaijia.userclient.util.GlobalPositionInfo;
import xps.and.uudaijia.userclient.view.base.BaseActivity;
import xps.and.uudaijia.userclient.view.fragment.MapViewFragment;

@RequiresPresenter(ObtainAddressPresenter.class)
public class ObtainAddressActivty extends BaseActivity<ObtainAddressPresenter> implements MapViewFragment.PinActivationListener, AdapterView.OnItemClickListener, View.OnClickListener {
    public MapViewFragment mapfragment;
    public ListView list_obtainaddress;
    public PoiListAdapter mAdapter;
    public PoiSearch mPoiSearch;
    @BindView(R.id.et_des)
    public EditText et_des;
    @BindView(R.id.tv_city_word)
    public TextView tv_city;
    @BindView(R.id.searchbar_container)
    public AutoLinearLayout searchbar_container;
    @BindView(R.id.al_choose_city)
    public AutoLinearLayout al_choose_city;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    public static String RESPONCE = "response";
    public static int REQUEST_CITY = 1;
    private int resultCode = 0;
    GlobalPositionInfo globalPositionInfo;

    @Override
    protected int getLayout() {
        return R.layout.activity_obtain_addr;
    }

    @Override
    protected void setData() {
        mapfragment.setPinVisibility(View.VISIBLE);
        list_obtainaddress.setAdapter(mAdapter);
        mapfragment.setPinActionListener(this);

    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        mAdapter = new PoiListAdapter(this);
        mapfragment = new MapViewFragment();
        al_choose_city.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        list_obtainaddress = (ListView) findViewById(R.id.list_obtainaddress);
        list_obtainaddress.setOnItemClickListener(this);
        mPoiSearch = PoiSearch.newInstance();
        CommonUtils.replaceFragment(R.id.container_map, mapfragment
                , getSupportFragmentManager());

        new CrazyShadow.Builder()
                .setContext(this)
                .setDirection(CrazyShadowDirection.BOTTOM)
                .setShadowRadius(JUtils.dip2px(3))
                .setCorner(JUtils.dip2px(10))
                .setBackground(getResources().getColor(R.color.white))
                .setImpl(CrazyShadow.IMPL_FLOAT)
                .action(searchbar_container);

        globalPositionInfo = App.getInstance().globalPositionInfo;
        if (globalPositionInfo != null && globalPositionInfo.bdLocation != null && !TextUtils.isEmpty(globalPositionInfo.bdLocation.getCity()))
            tv_city.setText(globalPositionInfo.bdLocation.getCity());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPoiSearch.destroy();
        mapfragment.setPinActionListener(null);
    }

    public void setSearchData(List<PoiInfo> pois) {
        if (pois == null || pois.size() <= 1) return;
        mAdapter.refreash(pois);
        mapfragment.moveCameraTo(pois.get(0).location);
    }

    /**
     * 当指针移动结束
     * */
    @Override
    public void onPinMoved(ArrayList<PoiInfo> list) {
        if (list == null || list.size() <= 1) return;
         mAdapter.refreash(list);
    }

    /**
     * 当指针移动结束
     * */
    @Override
    public void onPinMoveStart() {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra(RESPONCE, mAdapter.getDataset().get(position));
        setResult(resultCode, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.al_choose_city:
                Intent i = new Intent(this, CityChooseActivity.class);
                startActivityForResult(i, REQUEST_CITY);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CITY && data != null) {
            String cityName = data.getStringExtra(CityChooseActivity.RESPONCE_CITY);
            tv_city.setText(cityName);
        }
    }
}
